<template>
  <div class="app-container customer-management-wrap">
    <div class="page-header">
      <div class="page-title">
        <span class="title">允销管理</span>
      </div>
      <div class="header-right">
        <el-button style="padding: 0 !important" @click="clickImport"
          ><svg-icon icon-class="import" class="import"
        /></el-button>
        <!-- <el-button style="padding: 0 !important" @click="clickExport"
          ><svg-icon icon-class="exporter" class="import"
        /></el-button> -->
      </div>
    </div>
    <div class="mainBox">
      <div class="leftBox shadowShell">
        <el-tree :data="data" @node-click="handleNodeClick"></el-tree>
      </div>
      <div
        :class="isAssociation ? 'shadowShell spShell' : 'shadowShell rightBox'"
      >
        <div class="search-box">
          <el-button
            v-if="showdeleteBtn"
            style="
              height: 32px;
              margin-right: 10px;
              color: #5a607f !important;
              background-color: rgba(243, 244, 249, 0.9) !important;
              border-radius: 4px !important;
              border: 1px solid rgba(243, 244, 249, 0.9);
            "
            @click="deleteRow()"
          >
            <i class="el-icon-delete"></i>
          </el-button>

          <el-select
            v-if="openScreen"
            v-model="value"
            placeholder="请选择"
            style="width: 150px; margin-right: 10px"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>

          <div class="tabsBox" style="margin-left: 0">
            <!-- <span class="options-label">显示：</span> -->
            <el-button
              v-for="item in sceneList.filter((res) => res.isSystem != 0)"
              :key="item.sceneId"
              :class="avtiveSceneId == item.id ? 'activeScene' : 'scene'"
              @click="sceneClick(item, 'clearSceneName')"
              >{{ item.name }}</el-button
            >
            <el-dropdown trigger="click" style="margin-left: 10px">
              <el-button
                size="small"
                :type="activeSceneName ? 'activeScene' : 'scene'"
              >
                {{ activeSceneName
                }}<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown" class="commonDropdown">
                <el-dropdown-item
                  v-for="(item, index) in tabs.filter(
                    (res) => res.isSystem != 1
                  )"
                  :key="item.sceneId"
                  :class="avtiveSceneId == item.id ? 'activeScene' : 'scene'"
                  @click.native="sceneClick(item)"
                >
                  {{ item.name }}
                </el-dropdown-item>
                <el-dropdown-item class="last" @click.native="setScene()">
                  <i class="el-icon-s-tools"></i>
                  <span>场景设置</span>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>

          <div>
            <table-header-filters
              ref="tableHeaderFilters"
              @changeState="changeFlagState"
              @filter-change="filterChange"
              :addType="addType"
            />
          </div>
        </div>
        <div v-loading="loading" class="clueTable">
          <clueTable
            ref="clueTable"
            @selection-change="handleSelectionChange"
            :headData="columnList"
            @showFilter="showFilter"
            @showDrawer="showDrawerFun"
            :tableData="tableData"
            :total="total"
            @pagination="pagination"
            @star="starCustomer"
            :loadding="loadding"
          >
          </clueTable>
        </div>
      </div>
    </div>

    <!-- 客户info -->
    <customInfoDrawer
      ref="customInfoDrawer"
      @saveSubmit="saveSubmit"
      @updateCustomerByIds="updateCustomerByIds"
      @drawerInfo="drawerInfo"
    />
    <!-- 新建客户 -->
    <addDialog
      ref="addDialog"
      :action="createAction"
      @saveSubmit="saveSubmit"
      :addType="addType"
      dialogTitle="新建客户"
    />

    <sceneDialog
      ref="sceneDialog"
      :tabs="tabs"
      :addType="addType"
      @RefreshScene="RefreshScene"
    />
    <!-- 公海 -->
    <seaDialog ref="seaDialog" />
    <!-- 导入 -->
    <GlobalImport ref="GlobalImport"></GlobalImport>
    <!-- 导出 -->
    <GlobalExport ref="GlobalExport"></GlobalExport>
  </div>
</template>
<script>
import customInfoDrawer from "@/components/customerManage/customInfoDrawer/index.vue";
import addDialog from "@/components/DialogCustom/addDialog.vue";
import sceneDialog from "@/components/DialogCustom/sceneDialog.vue";
import seaDialog from "@/components/DialogCustom/seaDialog.vue";
import clueTable from "@/components/clueManage/clues/clueTable";
import TableHeaderFilters from "@/components/FieldFilters/TableHeaderFilters";
import {
  queryPageList,
  addCustomer,
  deleteByIds,
  starCustomer,
  update,
  updateCustomerByIds,
} from "@/api/customer/customer";
import { queryScene, queryListHead } from "@/api/customer/common";
//全局导入
import GlobalImport from "@/components/GlobalImportExport/GlobalImport.vue";
//全局导出
import GlobalExport from "@/components/GlobalImportExport/GlobalExport.vue";

export default {
  components: {
    customInfoDrawer,
    addDialog,
    clueTable,
    sceneDialog,
    TableHeaderFilters,
    seaDialog,
    GlobalImport,
    GlobalExport,
  },
  mixins: [],
  props: {
    isAssociation: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      data: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 2",
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
              children: [
                {
                  label: "三级 3-1-1",
                },
              ],
            },
            {
              label: "二级 3-2",
              children: [
                {
                  label: "三级 3-2-1",
                },
              ],
            },
          ],
        },
      ],
      search: "",
      sceneList: [],
      avtiveSceneId: "",
      activeSceneName: "场景名称",
      openScreen: false,
      tableData: [],
      columnList: [],
      total: 0,
      freezingColumn: [],
      createAction: {
        type: "save",
        id: "",
        data: {},
      },
      tabs: [],
      avtiveSceneId: "",
      addType: this.$GlobalStatus.tableType.Customer,
      showdeleteBtn: false,
      rowList: [],
      loading: false,
      rowIds: [],
      tableSelectionList: [],
      params: {
        label: this.$GlobalStatus.tableType.Customer,
        pageNum: 1,
        pageSize: 10,
        sceneId: undefined,
        search: "",
      },
      options: [],
      value: "",
      loadding: true,
    };
  },
  mounted() {
    this.queryListHead();
    this.queryList();
    this.queryScene();
  },
  methods: {
    handleNodeClick() {},
    clickExport() {},
    filterChange(formList) {
      this.params.searchList = formList;
      this.queryList();
    },
    starCustomer(row) {
      starCustomer(row.id)
        .then((res) => {
          if (res.code == 0) {
            this.$refs.clueTable.toggleStarOk(row);
          }
        })
        .catch((error) => {});
    },
    updateCustomerByIds(params) {
      updateCustomerByIds(params)
        .then((res) => {
          if (res.code == 0) {
            this.$refs.clueTable.toggleStarOk(row);
          }
        })
        .catch((error) => {});
    },
    showFilter(row) {
      this.$refs.tableHeaderFilters.showFilterClick(row);
    },
    btnSearch() {
      this.queryList();
    },
    btnSeaDialog() {
      this.$refs.seaDialog.showFun();
    },
    pagination(pageNum) {
      this.params.pageNum = pageNum;
      this.queryList();
    },
    handleSelectionChange(val) {
      this.rowList = val;
      if (val.length > 0) {
        this.showdeleteBtn = true;
      } else {
        this.showdeleteBtn = false;
      }
      this.rowIds = [];
      this.rowList.forEach((element) => {
        this.rowIds.push(element.id);
      });
    },
    drawerInfo(type, id) {
      if (type == "2") {
        this.deleteRow(id);
      }
      if (type == "1") {
        let ids = [];
        ids.push(id);
        this.$refs.seaDialog.showFun(ids);
      }
    },
    //删除row
    deleteRow(id) {
      console.log(this.rowList);
      let title = "确定删除选中数据吗?";
      if (id) {
        title = "确定删除该数据吗?";
      }
      this.$confirm(title, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          let params = {
            ids: this.rowIds,
          };
          let loading = undefined;
          if (id) {
            params.ids = [id];
            loading = this.$loading({
              lock: true,
              text: "Loading",
              spinner: "el-icon-loading",
              background: "rgba(0, 0, 0, 0.7)",
            });
          }
          deleteByIds(params)
            .then((res) => {
              this.loading = false;
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              if (id) {
                params.ids = [id];
                this.$refs.customInfoDrawer.close();
              }
              if (loading) {
                loading.close();
              }
              this.RefreshAddress();
            })
            .catch((error) => {
              this.loading = false;
              if (loading) {
                loading.close();
              }
            });
        })
        .catch(() => {
          this.loading = false;
          //   this.$message({
          //     type: 'info',
          //     message: '已取消删除'
          //   });
        });
    },
    RefreshScene() {
      this.queryScene();
    },
    queryListHead() {
      queryListHead(this.addType)
        .then((res) => {
          this.columnList = res.data;
          this.loadding = false;
        })
        .catch((error) => {});
    },
    queryScene() {
      queryScene(this.addType)
        .then((res) => {
          this.sceneList = res.data;
          this.tabs = this.sceneList;
          this.avtiveSceneId = this.tabs[0].id;
          // this.activeSceneName = this.tabs[0].isSystem == 1 ? '' : this.tabs[0].name;
        })
        .catch((error) => {});
    },
    queryList() {
      queryPageList(this.params)
        .then((res) => {
          this.tableData = res.data.records;
          this.total = Number.parseInt(res.data.total);
          console.log("tableData------", this.tableData);
        })
        .catch((error) => {
          this.loading = false;
        });
    },
    titleClick() {},
    //场景
    setScene() {
      this.$refs.sceneDialog.showFun();
    },
    //
    sceneClick(item) {
      this.params.sceneId = item.id;
      this.queryListHead();
      this.queryList();
      this.avtiveSceneId = item.id;
      // this.activeSceneName = item.isSystem == 1 ? '' : item.name;
      console.log(item.isSystem);
    },
    // 点击客户名称
    showDrawerFun(rowData) {
      this.$refs.customInfoDrawer.show(rowData);
    },
    cellClass({ row, column, rowIndex, columnIndex }) {
      if (column.property === "merchantNum") {
        return "look--underline";
      } else {
        return "";
      }
    },
    // 新建客户
    addCustomer() {
      this.$refs.addDialog.showFun();
    },
    saveSubmit(params) {
      if (params.entity.id) {
        update(params)
          .then((res) => {
            this.$refs.customInfoDrawer.closeAddFun();
            this.queryList();
          })
          .catch((error) => {
            this.$refs.addDialog.closeLoading();
          });
      } else {
        addCustomer(params)
          .then((res) => {
            this.$refs.addDialog.closeFun();
            this.queryList();
          })
          .catch((error) => {
            this.$refs.addDialog.closeLoading();
          });
      }
    },
    /**
     * 创建成功
     */
    saveSuccess(data) {
      if (data.type == "customer") {
        // this.handleHandle(data) ---刷新列表
        if (data.createContacts) {
          // this.createType = 'contacts'
          this.createAction = {
            type: "relative",
            crmType: "customer",
            data: {
              customer: data.data,
            },
          };
          this.createShow = true;
        }
      }
    },
    //更新下拉状态
    changeFlagState(val) {
      this.openScreen = val;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/custom-tab-serch.scss";
@import "@/assets/styles/pageStyle.scss";

.mainBox {
  display: flex;
  .leftBox {
    width: 200px;
    margin-right: 10px;
    height: calc(100vh - 160px);
    padding: 20px;
    background-color: white;
  }
  .rightBox {
    flex: 1;
  }
}
.spShell {
  height: initial !important;
}
.app-container {
  height: calc(100vh - 180px);
}
.shadowShell {
  height: calc(100vh - 160px);
  background-color: #ffffff;
  overflow-y: auto;
}
.customer-management-wrap {
  background-color: #f5f6fa;

  // .f14{
  //     font-size: 14px;
  // }
  .page-header {
    .page-title {
      display: flex;
      align-items: center;

      .title-icon {
        color: #42526e;
        cursor: pointer;
        margin-left: 4px;
        font-size: 16px;
      }
    }

    .header-right {
      .more {
        margin-left: 8px;
      }
    }
  }

  .regular-row {
    display: flex;
    flex-direction: row;
    text-align: left;
    align-items: center;
    width: 100%;

    .search-input {
      width: 200px;
    }

    .options-wrap {
      margin-left: 24px;
      display: flex;
      flex: 1;
      overflow-x: auto;
      white-space: nowrap;
      align-items: center;

      .options-label {
        margin-right: 8px;
      }
    }
  }

  .activeScene {
    color: #fff;
    background-color: #344563;
  }

  .senior-scene {
    margin-top: 8px;
    padding: 16px;
    box-shadow: 0 4px 8px -2px rgba(9, 30, 66, 0.25),
      0 0 1px rgba(9, 30, 66, 0.31);

    .senior-scene-title {
      color: #5e6c84;
    }

    .senior-scene-btn-wrap {
      margin-top: 10px;
    }
  }
}
</style>
